<?php

namespace app\modules\web\controllers;

use yii\web\Controller;
use \app\common\services\CityService;
use \app\modules\web\controllers\common\BaseController;
use \app\models\pay\PayOrderItem;
use \app\models\pay\PayOrder;
use \app\models\book\Book;
class FinanceController extends BaseController
{
   
    //订单列表
    public function actionIndex()
    {
    	
        //查找payorder的数据以pay_order_id为键值
        $list=PayOrder::find()->join('inner join','pay_order_item','pay_order.id=pay_order_item.pay_order_id')->select('pay_order.order_sn,pay_order.pay_price,pay_order.created_time,pay_order.pay_time,pay_order_item.pay_order_id,pay_order.id,pay_order.status')->asArray()->indexBy('pay_order_id')->all();
        foreach($list as $k=>$v){
            //组成book_map
            $book_map=PayOrderItem::find()->join('inner join','book','pay_order_item.target_id=book.id')->select('book.name,book.id,pay_order_item.quantity')->where(['pay_order_id'=>$k])->asArray()->all();
            $list[$k]['book_map']=$book_map;
        }
        // var_dump($list);die;
        
        return $this->render('index',['list'=>$list]);
    }
    //财务流水
	public function actionAccount()
    {
    	$list=PayOrder::find()->select('order_sn,pay_time,pay_price,status')->where(['status'=>1])->asArray()->all();
        $total=0;
        foreach($list as $v){
            $total+=$v['pay_price'];
        }
        return $this->render('account',['list'=>$list,'total'=>$total]);
    }
    //订单详情
    public function actionPay_info()
    {
        $id=$this->get('id','');
    	//查找payorder的数据以pay_order_id为键值
        $info=PayOrder::find()->join('inner join','pay_order_item','pay_order.id=pay_order_item.pay_order_id')->join('inner join','member_address','pay_order.express_address_id=member_address.id')->select('pay_order.order_sn,pay_order.pay_price,pay_order.created_time,pay_order.pay_time,pay_order.id,pay_order.status,pay_order.express_address_id,member_address.nickname,member_address.mobile')->where(['pay_order.id'=>$id])->asArray()->one();
        // var_dump($list);die;
       
            //组成book_map
            $book_map=PayOrderItem::find()->join('inner join','book','pay_order_item.target_id=book.id')->select('book.name,book.id,book.price,pay_order_item.quantity')->where(['pay_order_id'=>$info['id']])->asArray()->all();
            $info['book_map']=$book_map;
            $info['address']=CityService::getAddress($info['express_address_id']);
           // var_dump($info);die; 
        return $this->render('pay_info',['info'=>$info]);
    }
    //确认发货
    public function actionExpress(){
        $id=$this->post('id','');
        $express_info=$this->post('express_info','');
        if(!$id || !$express_info){
            return $this->renderJson([],'错误',-1);
        }
        $is_find=PayOrder::find()->where(['id'=>$id])->one();
        if(!$is_find){
            return $this->renderJson([],'错误',-1);
        }
        $is_find->express_status=-6;
        $is_find->express_info=$express_info;
        $is_find->updated_time=time();
        $is_find->save();
        if(!$is_find){
            return $this->renderJson([],'错误',-1);
        }
        return $this->renderJson([],'发货成功');
    }
}
